package cn.wps.moffice.main.local.filebrowser.operator.seek;

import java.io.File;
import java.util.ArrayList;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import cn.wps.moffice.main.local.filebrowser.operator.fscache.DataCenter;
import cn.wps.moffice.main.local.filebrowser.operator.fscache.Digger;
import cn.wps.moffice.main.local.filebrowser.operator.fscache.IFileScanner;

/**
 * 文件搜素 抽象类, 代理 Digger 的业务接口.
 *
 * 为子类 FsFinder 提供消息管理器. 通知View/Controller进行数据更新.
 * FileBroserOperator 调用.
 *
 * @author wangxiaohua
 *
 */
public abstract class AbstractFinder implements IFinder
{
	public abstract void dispose();

	protected final String DOWNLOADPATH = "Download";

	protected final int MSG_REFLESH = 0;
	protected final int MSG_FINISH = 1;
	protected final int MSG_FORCE_REFRESH = 2;

	protected IFileScanner fs = null;
	protected SeekHandler mSeekHandler;
	// protected AtomicInteger mToken = new AtomicInteger(1);

	protected ArrayList<SeekCallback> callbacks = new ArrayList<IFinder.SeekCallback>();

	protected Context mContext;

	protected long seekTime = 0;

	public AbstractFinder()
	{
		mSeekHandler = new SeekHandler(this);
		fs = Digger.getInstance();
	}

	//观察者模式
	public void addSeekCallback(SeekCallback callback)
	{
		synchronized (callbacks)
		{
			if (!callbacks.contains(callback)) {
				callbacks.add(callback);
			}
		}
	}

	public void removeSeekCallback(SeekCallback callback)
	{
		synchronized (callbacks)
		{
			if (callbacks.contains(callback))
			{
				callbacks.remove(callback);
				callback.dispose();
			}
		}
	}

	protected ArrayList<SeekCallback> getCallbacks()
	{
		synchronized (callbacks)
		{
			@SuppressWarnings("unchecked")
			ArrayList<SeekCallback> clone = (ArrayList<SeekCallback>)callbacks.clone();
			return clone;
		}
	}

	// 负责 消息的处理.
	class SeekHandler extends Handler
	{
		private AbstractFinder finder;

		public SeekHandler(AbstractFinder finder)
		{
			super(Looper.getMainLooper());
			this.finder = finder;
		}

		@Override
		public void handleMessage(Message msg)
		{
			ArrayList<SeekCallback> callbacks = finder.getCallbacks();

			switch (msg.what)
			{
			case MSG_REFLESH:
				for (int i = 0; i < callbacks.size(); i++)
				{
					callbacks.get(i).refresh((SeekType)msg.obj, msg.arg1);
				}
				break;
			case MSG_FINISH:
				int dirtyFlag = DataCenter.getInstance().getDirtFlag();
				DataCenter.getInstance().clearDirtyAll();

				for (int i = 0; i < callbacks.size(); i++)
				{
					callbacks.get(i).seekFinish((SeekType)msg.obj, dirtyFlag);
				}
				DataCenter.getInstance().setFirstSeekDone();

				break;
			case MSG_FORCE_REFRESH:
				for (int i = 0; i < callbacks.size(); i++)
				{
					callbacks.get(i).refresh((SeekType)msg.obj, Integer.MAX_VALUE);
				}
				break;
			}
		}
	}

	protected boolean isParentHidden(File file)
	{
		File f = new File(file.getPath());
		File pf = f.getParentFile();
		while(null != pf && pf.exists())
		{
			if (pf.isHidden())
			{
				return true;
			}
			else
			{
				pf = pf.getParentFile();
			}
		}
		return false;
	}

}